package weifei.wx.community.service;


import com.mybatisflex.core.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import weifei.wx.community.entity.Avatar;
import weifei.wx.community.entity.dto.UserDTO;
import weifei.wx.community.mapper.AvatarMapper;
import weifei.wx.community.mapper.UserMapper;



@Service
public class UserService
{
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private AvatarMapper avatarMapper;


    public UserService()
    {
    }


    public UserDTO getUserByID(Long id)
    {
        QueryWrapper queryWrapper = QueryWrapper.create()
                .select()
                .where("id = ?", id);
        var user = userMapper.selectOneByQuery(queryWrapper);
        if (user == null)
            return null;

        var avatar = this.getAvatarByUserID(user.getId());
        return new UserDTO(user, avatar);
    }
    public UserDTO getUserByWXOpenid(String wxOpenid)
    {
        QueryWrapper queryWrapper = QueryWrapper.create()
                .select()
                .where("wx_openid = ?", wxOpenid);
        var user = userMapper.selectOneByQuery(queryWrapper);
        if (user == null)
            return null;

        var avatar = this.getAvatarByUserID(user.getId());
        return new UserDTO(user, avatar);
    }


    public Avatar getAvatarByUserID(Long userID)
    {
        QueryWrapper queryWrapper = QueryWrapper.create()
                .select()
                .where("user_id = ?", userID);
        return avatarMapper.selectOneByQuery(queryWrapper);
    }
}


